<?php

namespace app\impl;

use app\common\lib\PushMessage;
use app\common\lib\ZhyxBeanstalkd;
use app\common\lib\ZhyxMongo;
use app\common\model\BaseModel;
use app\common\model\BindDiseasePath;
use app\common\model\DiseasePath;
use app\common\model\DiseasePathBind;
use app\common\model\DiseasePatientTask;
use app\common\model\DiseasePatientTaskDetail;
use app\common\model\DiseaseTask;
use app\common\model\PatientInfo;
use app\interfaces\Task;
use app\common\model\PropagandaEducationHistory as PehModel;
use app\common\model\PushMessage as PmModel;
use catchAdmin\propaganda\service\EdCenterService;
use catchAdmin\registration\service\TaskService;
use catchAdmin\visitPlan\model\VisitPlan;
use catcher\Utils;
use MongoDB\BSON\ObjectId;
use think\facade\Log;

class PushDiseaseTask implements Task
{

    public function run(array $data, string $taskId): ?bool
    {

        $outerId = "";
        $pushType = Utils::getPushType($data["push_type"]);
        try {
            switch ($data["task_type"]) {
                case "content":
                    $result = (new EdCenterService())->automaticPushEd($data["patient_id"], $data["attachment_parent_value"], $data["attachment_value"], $pushType);
                    break;
                case "form":
                    $result = (new TaskService())->automaticPushFollow($data["patient_id"], $data["attachment_value"], $data['sms_id'] ?? 0, $pushType);
                    break;
                case "plan":

                    $result = (PushMessage::getInstance())->send([
                        'type' => 4,
                        'source' => '复诊提醒',
                        'task_id' => $data['task_main_id'],
                        'template_id' => $data['sms_id'],
                        'business_id' => '',
                        'users' => [$data["patient_id"]],
                        'tubeName' => 'SendSms',
                        'userInfo' => [
                            'id' => '1',
                            'username' => '系统自动推送',
                            'doctor_code' => '',
                            'dept_name' => '系统自动推送'
                        ],
                        'model' => 'Path',
                        'task_name' => '复诊提醒'

                    ], $data['push_type'] ?? "ALL");

                    break;
                default:
                    $outerId = "";
                    break;
            }
            if (!empty($result)) {
                $outerId = $result["history_id"] ?? "";
                # 将本条任务更新为进行中
                (new DiseasePatientTaskDetail())->where([["id", "=", new ObjectId($data["id"])]])->save([
                    'outer_id' => $outerId,
                    'creator_id' => 0,
                    'creator_by' => '系统',
                    'latest_status' => 1
                ]);
            }
        } catch (\Exception $exception) {
            Log::error("随访路径自动任务推送失败，失败原因:" . $exception->getMessage() . $exception->getFile() . $exception->getLine());
            return true;
        }
        return true;
    }
}